package controllers.bluehat.backbone;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import utils.exceptions.ValidationException;
import utils.json.data.AbstractJson;
import utils.json.data.backbone.InfoRequestJson;
import utils.json.data.backbone.InfoUnitJson;

import models.AbstractUnit;
import models.Focus;
import models.InformationRequest;
import models.InformationUnit;
import controllers.BaseController;
import controllers.bluehat.BlueHatBaseController;
import controllers.bluehat.backbone.FocusController.ErrorJson;

public class InformationRequestController extends BlueHatBaseController {

	public static void list(Long focusId, String open, String remain, String closed) {
		Focus focus = Focus.getById(focusId, Focus.findByOwner(getCurrentUser()));
		List<InformationRequest> informations = new ArrayList<InformationRequest>();
		
		if(!isBlank(open)) informations.addAll(InformationRequest.findAllOpen(getCurrentDate(), focus.informationRequest));
		if(!isBlank(remain)) informations.addAll(InformationRequest.findAllRemaining(getCurrentDate(), focus.informationRequest));
		if(!isBlank(closed)) informations.addAll(InformationRequest.findAllClose(getCurrentDate(), focus.informationRequest));
		
		List<InfoRequestJson> infoJson = new ArrayList<InfoRequestJson>();
		for(InformationRequest information : informations) {
			infoJson.add(new InfoRequestJson(information));
		}
		
		renderJSON(infoJson);
	}
	
	public static void save(Long focusId, Long id, String body) throws ValidationException {
		Focus focus = Focus.getById(focusId, Focus.findByOwner(getCurrentUser()));
		
		InformationRequest information = null;
		if(id != null) {
			information = InformationRequest.getById(id, focus.informationRequest); 
		} else {
			information = new InformationRequest();
			information.focus = focus;
		}
		
		JsonObject json = new JsonParser().parse(body).getAsJsonObject();
		String description = json.get("description") != null ? json.get("description").getAsString() : null;
		Long start = !isNull(json.get("start")) ? json.get("start").getAsLong() : null; 
		Long end = !isNull(json.get("end")) ? json.get("end").getAsLong() : null;
		
		information.description = description;
		information.start = start != null ? new Date(start) : null;
		information.end = end != null ? new Date(end) : null;
		
		validation.valid(information);
		if(validation.hasErrors()) {
			ErrorJson errors = new ErrorJson(validation.errors());
			throw new ValidationException("Validation failed!!", errors);
			//JsonObject test = new JsonParser().parse(errors.toJson()).getAsJsonObject();
			//error(errors.toJson());
		} else {
			information.save();
			ok();
		}		
	}
	
	public static void delete(Long focusId, Long id) {
		Focus focus = Focus.getById(focusId, Focus.findByOwner(getCurrentUser())); 
		InformationRequest infoRequest = InformationRequest.getById(id, focus.informationRequest);
		
		infoRequest.delete();
	}
	
	public static void units(Long infoId) {
		InformationRequest infoRequest = InformationRequest.getById(infoId, null);
		List<InfoUnitJson> unitsJson = new ArrayList<InfoUnitJson>();
		
		for(InformationUnit unit : infoRequest.informations.fetch()) {
			unitsJson.add(new InfoUnitJson(unit));
		}
		renderJSON(unitsJson);
	}

	public static void detail(Long infoId) {
		InformationRequest infoRequest = InformationRequest.getById(infoId, null);
		
		renderJSON(new InfoRequestJson(infoRequest));
	}
}
